// 封装购物车相关接口
import request from "@/utils/request";

// 加入购物车
export const insertCartAPI = ({ skuId, count }) => {
  console.log('发送加入购物车请求:', { skuId, count });
  return request({
    url: `/member/cart`,
    method: "POST",
    data: {
      skuId,
      count,
    },
  }).then(res => {
    console.log('加入购物车响应:', res);
    return res;
  }).catch(err => {
    console.error('加入购物车失败:', err);
    throw err;
  });
};

// 获取购物车列表
export const getNewCartListAPI = () => {
  console.log('发送获取购物车列表请求');
  return request({
    url: `/member/cart`,
  }).then(res => {
    console.log('获取购物车列表响应:', res);
    return res;
  }).catch(err => {
    console.error('获取购物车列表失败:', err);
    throw err;
  });
};

// 删除购物车
export const delCartAPI = (ids) => {
  console.log('发送删除购物车请求:', { ids });
  return request({
    url: `/member/cart`,
    method: "DELETE",
    data: {
      ids,
    },
  }).then(res => {
    console.log('删除购物车响应:', res);
    return res;
  }).catch(err => {
    console.error('删除购物车失败:', err);
    throw err;
  });
};

// 合并购物车 本地与当前账号合并
export const mergeCartAPI = (data) => {
  console.log('发送合并购物车请求:', data);
  return request({
    url: `/member/cart/merge`,
    method: "POST",
    data,
  }).then(res => {
    console.log('合并购物车响应:', res);
    return res;
  }).catch(err => {
    console.error('合并购物车失败:', err);
    throw err;
  });
};

// 更新购物车商品数量
export const updateCartCountAPI = ({ skuId, count }) => {
  console.log('发送更新购物车商品数量请求:', { skuId, count });
  return request({
    url: `/member/cart`,
    method: "PUT",
    data: {
      skuId,
      count,
    },
  }).then(res => {
    console.log('更新购物车商品数量响应:', res);
    return res;
  }).catch(err => {
    console.error('更新购物车商品数量失败:', err);
    throw err;
  });
};